package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CusterException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

/**
 * <p>
 * 菜品管理 服务实现类
 * </p>
 *
 * @author 黑马程序员
 * @since 2022-06-06
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;

    @Transactional
    public void saveByDto(DishDto dto) {
        super.save(dto);    //1)现在保存Dish对象到数据库, 多余的字段会忽略掉
        Long id = dto.getId();//菜品id

        List<DishFlavor> flavors = dto.getFlavors();

        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }

        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 通过id查询进行数据回显
     *
     * @param id
     * @return
     */
    public DishDto getByDishId(Long id) {

        DishDto dishDto = new DishDto();
        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish, dishDto);

        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(qw);
        dishDto.setFlavors(list);
        return dishDto;
    }

    /**
     * 更新
     *
     * @param dishDto
     */
    public void updateByDto(DishDto dishDto) {
        //更新信息表的相关信息
        this.updateById(dishDto);
        //删除之前的口味信息
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper();
        qw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(qw);

        //添加新的口味信息
        Long id = dishDto.getId();//菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }
        dishFlavorService.saveOrUpdateBatch(flavors);
    }

   @Transactional
    public void deleteByids(List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Dish::getId, ids);
        queryWrapper.eq(Dish::getStatus, 1);
        int count = dishService.count(queryWrapper);
        if (count > 0) {
           throw new CusterException("无法删除");
        }
        this.removeByIds(ids);
        dishService.removeByIds(ids);
        LambdaQueryWrapper<DishFlavor> queryWrapper2 = new LambdaQueryWrapper();
        queryWrapper2.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(queryWrapper2);
    }


}
